//
//  SSSRecordOrderToDownlinePayViewController.m
//  SSSMerchant
//
//  Created by 谢曦 on 2017/3/25.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSRecordOrderToDownlinePayViewController.h"
#import "ImagePickerTool.h"
@interface SSSRecordOrderToDownlinePayViewController()<ImagePickerToolDelegate>
{
    NSArray *_dataArr;
    UIButton *_addButton;
}
@end

@implementation SSSRecordOrderToDownlinePayViewController
-(void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.title=@"线下支付";
    SSSCellBaseConfigModel *merchantName=[[SSSCellBaseConfigModel alloc]init];
    merchantName.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    merchantName.title=@"商家名称";
    merchantName.identifier=@"name";
    merchantName.rightText=_model.name;
    
    
    SSSCellBaseConfigModel *legalPerson=[[SSSCellBaseConfigModel alloc]init];
    legalPerson.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    legalPerson.title=@"企业法人";
    legalPerson.identifier=@"personName";
    legalPerson.rightText=_model.personName;
    
    SSSCellBaseConfigModel *totalPay=[[SSSCellBaseConfigModel alloc]init];
    totalPay.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    totalPay.title=@"支付金额";
    totalPay.identifier=@"totalPayMoney";
    totalPay.rightText=[NSString stringWithFormat:@"¥%.2f",_totalPayMoney];
    
    _dataArr=@[merchantName,legalPerson,totalPay];
    [self.tableView reloadData];
   
   
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return section==0?_dataArr.count:1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SSSInfoBaseTableViewCell *cell=[SSSInfoBaseTableViewCell cellForTableView:tableView withIdentifier:@"SSSInfoBaseTableViewCell"];
    if (indexPath.section==0) {
        cell.configModel=_dataArr[indexPath.row];
        return cell;
    }
    else {
        return [self cellForAddPhoto];
    };
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
      return section==1?kFooterH:0.1;;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section==1?kHeaderH:0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section==0? kRowH:120;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return section==0?[UIView new]:[self buttonFooterWithTitle:@"提交"];

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return section==0?[UIView new]:[self headerWithTitle:@"上传凭据"];

}


#pragma mark - ImagePickerToolDelegate
-(void)imagePickerControllerDidCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didPicker:(UIImage *)image userInfo:(NSDictionary *)info
{
    
    [_addButton setImage:image forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - action
-(void)addPhoto{
    XXSheetView *sheet=[XXSheetView sheetViewWithTitle:@"选择图片来源" redButtonTitle:@"" otherbuttonTitle:@[@"照相机",@"相册"]];
    
    [ImagePickerTool sharedManager].delegate=self;
    sheet.sheetViewOtherButtonClickBlock=^(NSUInteger index,NSString *title){
        if(index==1){
            //相册
            if ([[ImagePickerTool sharedManager] sourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum ]==YES) {
                UIImagePickerController *imagePicker=[ImagePickerTool  imagePickerController];
                
                [self presentViewController:imagePicker animated:YES completion:nil];
            }
            
        }
        if(index==0){
            //相机
            if ([[ImagePickerTool sharedManager] sourceTypeAvailable:UIImagePickerControllerSourceTypeCamera ]==YES) {
                UIImagePickerController *cameraController=[ImagePickerTool  cameraController];
                [self presentViewController:cameraController animated:YES completion:nil];
                
            }
        }
        
    };
    [sheet showSheetView];
    


}
#pragma mark - public

-(void)footerButtonClick{
    [self.view endEditing:YES];
    
    
    
}
-(UITableViewCell *)cellForAddPhoto{
    
    UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
    }
    cell.backgroundColor=[UIColor whiteColor];
    _addButton=[UIButton new];
    [_addButton setImage:[UIImage imageNamed:@"img_adphoto"] forState:UIControlStateNormal];
    [_addButton addTarget:self action:@selector(addPhoto) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:_addButton];
    [_addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(91, 91));
    }];
//    cell.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);
    return cell;
}

@end
